/**
 * 常用工具方法
 */

/**
 * 格式化时间
 * @param {any} dateTime 时间
 * @returns 格式化后的时间
 */
export function formatDate(dateTime) {
    if (!dateTime) return '';
    
    let date = new Date(dateTime);
    let y = date.getFullYear();
    let m = (date.getMonth() + 1).toString().padStart(2, '0');
    let d = date.getDate().toString().padStart(2, '0');
    let h = date.getHours().toString().padStart(2, '0');
    let mi = date.getMinutes().toString().padStart(2, '0');
    let s = date.getSeconds().toString().padStart(2, '0');
    return `${y}-${m}-${d} ${h}:${mi}:${s}`;
}

/**
 * 读取Cookie，获取指定key对应的值
 * @param {string} key Cookie中的key
 */
export function getValueFromCookie(key) {
    const keyIndex = document.cookie.indexOf(key);
    if (keyIndex === -1) return '';

    const start = document.cookie.indexOf('=', keyIndex);
    let end = document.cookie.indexOf(';', start);
    if (end === -1) end = undefined;
    return document.cookie.substring(start + 1, end);
}